package chis.dip.map;

import chis.dip.fragments.MyMapFragment;
import chis.dip.util.LocationData;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMapOptions;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.MarkerOptions;

import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity {

	//	MapFragment mMapFragment;

	MyMapFragment myMapFragment;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		//addMapFragment();
		
		Intent intent = new Intent(this, MapActivity.class);
		startActivity(intent);
		




		//		 mMapFragment = MapFragment.newInstance();
		//		 FragmentTransaction fragmentTransaction =
		//		         getFragmentManager().beginTransaction();
		//		 fragmentTransaction.add(android.R.id.content, mMapFragment, "m");
		//		 //fragmentTransaction.add(R.id.map, mMapFragment);
		//		 fragmentTransaction.commit();
		//		 
		//		 Boolean b = getFragmentManager().executePendingTransactions();
		//		 Log.d("had to commit", b.toString());

		//		 if (getFragmentManager().findFragmentByTag("m") == null) {
		//			 Log.d("XXXXXXXXXXXXXXXXXXXXXXXX","xx");
		//		 }

		//		GoogleMap mMap;
		////		mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
		//		mMap = ((MapFragment) getFragmentManager().findFragmentByTag("m")).getMap();
		//		
		//		if (mMap != null) {
		//			 Log.d("XXXXXXXXXXXXXXXXXXXXXXXX","xx");
		//		} else {
		//				 int checkGooglePlayServices = GooglePlayServicesUtil
		//				               .isGooglePlayServicesAvailable(this);
		//				        if (checkGooglePlayServices != ConnectionResult.SUCCESS) {
		//				               // google play services is missing!!!!
		//				               /*
		//				                * Returns status code indicating whether there was an error.
		//				                * Can be one of following in ConnectionResult: SUCCESS,
		//				                * SERVICE_MISSING, SERVICE_VERSION_UPDATE_REQUIRED,
		//				                * SERVICE_DISABLED, SERVICE_INVALID.
		//				                */
		//				               GooglePlayServicesUtil.getErrorDialog(checkGooglePlayServices,
		//				                      this, 10).show();
		//				        }
		//				        
		//				        Toast.makeText(getApplicationContext(), 
		//				        	     "isGooglePlayServicesAvailable SUCCESS", 
		//				        	     Toast.LENGTH_LONG).show();				        
		//		}
		//		
		////		mMap.addMarker(new MarkerOptions()
		////        .position(new LatLng(37.8136, 144.9631))
		////        .title("Hello world"));

	}

	public void addMapFragment() {
		myMapFragment = new MyMapFragment();
		FragmentTransaction fragmentTransaction =
				getFragmentManager().beginTransaction();
		fragmentTransaction.add(android.R.id.content, myMapFragment.getmMapFragment(), myMapFragment.getFragmentTag());
		fragmentTransaction.commit();

		Boolean b = getFragmentManager().executePendingTransactions();
		Log.d("had to commit", b.toString());
	}

	private GoogleMap getMapFromFragment() {
		GoogleMap mMap = ((MapFragment) getFragmentManager().findFragmentByTag(myMapFragment.getFragmentTag())).getMap();
		return mMap;
	}

//	@Override
//	public void onStart() {
//		super.onStart();
//
//		GoogleMap mMap = getMapFromFragment();
//
//		if (mMap != null) {
//			Log.d("XXXXXXXXXXXXXXXXXXXXXXXX","xx");
//		} else {
//			int checkGooglePlayServices = GooglePlayServicesUtil
//					.isGooglePlayServicesAvailable(this);
//			if (checkGooglePlayServices != ConnectionResult.SUCCESS) {
//				// google play services is missing!!!!
//				/*
//				 * Returns status code indicating whether there was an error.
//				 * Can be one of following in ConnectionResult: SUCCESS,
//				 * SERVICE_MISSING, SERVICE_VERSION_UPDATE_REQUIRED,
//				 * SERVICE_DISABLED, SERVICE_INVALID.
//				 */
//				GooglePlayServicesUtil.getErrorDialog(checkGooglePlayServices,
//						this, 10).show();
//			}
//
//			Toast.makeText(getApplicationContext(), 
//					"isGooglePlayServicesAvailable SUCCESS", 
//					Toast.LENGTH_LONG).show();				        
//		}
//
//		mMap.setMyLocationEnabled(true);
//
//		mMap.addMarker(new MarkerOptions()
//		.position(LocationData.DANDENONG)
//		.title("Dandenong")
//		.snippet("Dandenong 3175"));
//
//		mMap.addMarker(new MarkerOptions()
//		.position(LocationData.MELBOURNE)
//		.title("Melbourne")
//		.snippet("Melbourne 3000"));
//
//		mMap.addMarker(new MarkerOptions()
//		.position(LocationData.SYDNEY)
//		.title("Sydney")
//		.snippet("Sydney 2000"));
//
//		mMap.addMarker(new MarkerOptions()
//		.position(LocationData.CHISHOLM_AT_311)
//		.title("Chisholm @311")
//		.snippet("Lonsdale Street Dandenong 3175")
//		.icon(BitmapDescriptorFactory.fromAsset("markers/university.png")));
//		// .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
//
//		mMap.addMarker(new MarkerOptions()
//		.position(LocationData.CHISHOLM_AT_STUD_RD)
//		.title("Chisholm Dandenong")
//		.snippet("Stud Road Dandenong 3175"));
//
//		mMap.addMarker(new MarkerOptions()
//		.position(LocationData.CHISHOLM_AT_CRANBOURNE)
//		.title("Chisholm Cranbourne")
//		.snippet("New Holland Drive Cranbourne 3177"));
//
//		mMap.addMarker(new MarkerOptions()
//		.position(LocationData.CHISHOLM_AT_FRANKSTON)
//		.title("Chisholm Frankston")
//		.snippet("Fletcher Road Frankston 3199"));
//
//		mMap.addCircle(new CircleOptions()
//		.center(LocationData.DANDENONG)
//		.radius(1200)
//		.fillColor(0x333300FF)
//		.strokeColor(0xFF3300FF)
//		.strokeWidth(1.2f));
//
////		mMap.getUiSettings().setCompassEnabled(true);
////		mMap.getUiSettings().setRotateGesturesEnabled(true);
//
//		mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LocationData.MELBOURNE, 10.0f));
//
//
//
//		//		LatLngBounds AUSTRALIA = new LatLngBounds(
//		//				  new LatLng(-44, 113), new LatLng(-10, 154));
//		//
//		//				// Set the camera to the greatest possible zoom level that includes the
//		//				// bounds
//		//				mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(AUSTRALIA, 100));	
//	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
